<?php

namespace App\Http\Requests\Backend;

use App\Models\WechatMessage;
use Illuminate\Foundation\Http\FormRequest;

/**
 * Class StoreWechatMessage
 * @package App\Http\Requests\Backend
 */
class StoreWechatMessage extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if ($this->type == WechatMessage::RESPONSE_TYPE_KEY) {
            if ($this->message_type == WechatMessage::MSG_TEXT) {
                return WechatMessage::$keyTextRules;
            } elseif ($this->message_type == WechatMessage::MSG_IMAGE) {
                return WechatMessage::$keyImageRules;
            } else {
                return WechatMessage::$keyTextImageRules;
            }
        } else {
            if ($this->message_type == WechatMessage::MSG_TEXT) {
                return WechatMessage::$receiveTextRules;
            } elseif ($this->message_type == WechatMessage::MSG_IMAGE) {
                return WechatMessage::$receiveImageRules;
            } else {
                return WechatMessage::$receiveTextImageRules;
            }
        }
    }

    /**
     * @return array
     */
    public function attributes()
    {
        return [
            'name' => '规则名称',
            'type' => '回复类型',
            'key' => '关键字',
            'message_type' => '回复消息类型',
            'title' => '消息文字',
            'image_id' => '素材id',
            'description' => '描述',
            'image_url' => '图片链接',
            'url' => '链接'
        ];
    }

    /**
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '请填写:attribute',
            'type.required' => '请选择:attribute',
            'key.required' => '请填写:attribute',
            'message_type.required' => '请选择:attribute',
            'title.required' => '请填写:attribute',
            'image_id.required' => '请填写:attribute',
            'description.required' => '请填写:attribute',
            'image_url.required' => '请填写:attribute',
            'image_url.url' => ':attribute格式错误',
            'url.required' => '请填写:attribute',
            'url.url' => ':attribute格式错误'
        ];
    }
}
